package org.openea.eap.module.obpm.util.custsql;

import cn.hutool.core.exceptions.UtilException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.openbpm.base.core.util.StringUtil;
import org.openea.eap.module.obpm.model.custsql.FormCustSqlFields;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;









public class TuoMingUtil
{
  private static final Integer PHONE_LEN = Integer.valueOf(11);

  private static final Integer TELPHONE_LEN = Integer.valueOf(10);

  private static final Integer ID_CARD_LEN = Integer.valueOf(18);

  private static final Integer NEED_DESENSITIZATION_ADDRESS_LEN = Integer.valueOf(5);

  private static final Integer NEED_DESENSITIZATION_QQ_LEN = Integer.valueOf(5);

  private static final Integer NEED_DESENSITIZATION_WECHAT_LEN = Integer.valueOf(5);
















  public static void tuoMing(List<FormCustSqlFields> commonFieldList, JSONArray listData) {
    for (FormCustSqlFields fieldSetting : commonFieldList) {
      String fuzz = fieldSetting.getFuzz();
      if (StringUtil.isNotEmpty(fuzz))
      {
        customerFuzz(listData, fieldSetting.getFieldName(), fuzz);
      }
    }
  }




  private static void customerFuzz(JSONArray listData, String fieldName, String fuzz) {
    String[] fuzzSplit = fuzz.split(",");

    int fuzzSize = fuzzSplit.length;

    int left = Integer.parseInt(fuzzSplit[0]);
    if (left <= 0) {
      left = 0;
    }


    int right = 0;
    String xing = "**";


    if (fuzzSize == 2) {
      right = Integer.parseInt(fuzzSplit[1]);
      if (right <= 0) {
        right = 0;
      }
      xing = "****";
    }

    if (fuzzSize == 3) {
      right = Integer.parseInt(fuzzSplit[1]);
      if (right <= 0) {
        right = 0;
      }
      xing = "********************".substring(0, Integer.parseInt(fuzzSplit[2]));
    }

    for (Object dataJson : listData) {
      JSONObject dataJo = (JSONObject)dataJson;
      if (dataJo.containsKey(fieldName) && StringUtils.isNotEmpty(dataJo.getString(fieldName))) {
        String value = dataJo.getString(fieldName);
        String valueStr = value.toString();
        int valueStrLength = valueStr.length();


        if (left >= valueStrLength) {
          continue;
        }


        int size = left + right;
        if (size >= valueStrLength) {

          valueStr = valueStr.substring(0, left) + xing;
          dataJo.put(fieldName, valueStr);

          continue;
        }
        if (right == 0) {
          valueStr = valueStr.substring(0, left) + xing;
        } else {

          valueStr = valueStr.substring(0, left) + xing + valueStr.substring(valueStrLength - right, valueStrLength);
        }

        dataJo.put(fieldName, valueStr);
      }
    }
  }


  public static String tuoMing(String data, int tuoMingType) {
    switch (tuoMingType) {

      case 1:
        return desensPhone(data);

      case 2:
        return desensTelPhone(data);

      case 3:
        return desensIdCard(data);

      case 4:
        return desensName(data);

      case 5:
        return desensBankCardNo(data);

      case 6:
        return desensAddress(data);

      case 7:
        return desensQQ(data);

      case 8:
        return desensWechat(data);
    }
    throw new UtilException("暂不支持该脱敏类型" + tuoMingType);
  }








  public static String desensPhone(String phone) {
    if (StringUtils.isBlank(phone)) {
      return phone;
    }
    phone = phone.replaceAll(" ", "");
    if (StringUtils.isBlank(phone) || phone.length() != PHONE_LEN.intValue()) {
      return phone;
    }
    return (new StringBuilder(phone)).replace(3, 7, "****").toString();
  }







  public static String desensName(String bankCardNo) {
    if (StringUtils.isBlank(bankCardNo)) {
      return bankCardNo;
    }
    return (new StringBuilder(bankCardNo)).replace(1, bankCardNo.length(), "****").toString();
  }







  public static String desensBankCardNo(String bankCardNo) {
    if (StringUtils.isBlank(bankCardNo) || bankCardNo.length() < 11) {
      return bankCardNo;
    }
    return (new StringBuilder(bankCardNo)).replace(3, 7, "****").toString();
  }







  public static String desensTelPhone(String phone) {
    if (StringUtils.isBlank(phone) || phone.length() < TELPHONE_LEN.intValue()) {
      return phone;
    }
    return (new StringBuilder(phone)).replace(7, 9, "***").toString();
  }







  public static String desensIdCard(String idCard) {
    if (StringUtils.isBlank(idCard) || idCard.length() != ID_CARD_LEN.intValue()) {
      return idCard;
    }
    return (new StringBuilder(idCard)).replace(6, 14, "********").toString();
  }







  public static String desensAddress(String address) {
    int addressLen;
    if (StringUtils.isBlank(address) || (addressLen = address.length()) < NEED_DESENSITIZATION_ADDRESS_LEN.intValue()) {
      return address;
    }
    return (new StringBuilder(address)).replace(addressLen - 5, addressLen, "*****").toString();
  }







  public static String desensQQ(String qq) {
    int qqLen;
    if (StringUtils.isBlank(qq) || (qqLen = qq.length()) < NEED_DESENSITIZATION_QQ_LEN.intValue()) {
      return qq;
    }
    return (new StringBuilder(qq)).replace(2, qqLen - 3, makeRepeatableStar(qqLen - 5)).toString();
  }







  public static String desensWechat(String wechat) {
    int wechatLen;
    if (StringUtils.isBlank(wechat) || (wechatLen = wechat.length()) < NEED_DESENSITIZATION_WECHAT_LEN.intValue()) {
      return wechat;
    }
    return (wechatLen == PHONE_LEN.intValue()) ? desensPhone(wechat) : desensQQ(wechat);
  }








  private static String makeRepeatableStar(int repeatCount) { return makeRepeatableStr("*", repeatCount); }









  private static String makeRepeatableStr(String str, int repeatCount) {
    if (StringUtils.isBlank(str)) {
      return "";
    }
    StringBuilder sb = new StringBuilder(repeatCount);
    for (int i = 0; i < repeatCount; i++) {
      sb.append(str);
    }
    return sb.toString();
  }




  private static final List<String> PHONE_FIELD_LIST = Arrays.asList(new String[] { "phone", "telephoneNumber", "telephone", "mobile", "mobileNumber", "mobilePhone", "debtorPhone", "storeContactPhone", "phoneNumber" });





  private static final List<String> ID_CARD_FIELD_LIST = Arrays.asList(new String[] { "idCard", "certificateId", "ecodeOrIdcard", "idNumber", "idcard", "splitIdCard" });





  private static final List<String> ADDRESS_LIST = Arrays.asList(new String[] { "address", "debtorAddress" });




  private static final List<String> QQ_LIST = Arrays.asList(new String[] { "qq" });




  private static final List<String> TELPHONE_LIST = Arrays.asList(new String[] { "companyPhoneNumber", "workPhone" });




  private static final List<String> WECHAT_LIST = Arrays.asList(new String[] { "wechat" });







  public static void doKeywordsDesensitization(List<Object> data) throws IllegalAccessException {
    for (Object obj : data) {
      Field[] fields = obj.getClass().getDeclaredFields();
      for (Field field : fields) {
        field.setAccessible(true);
        String fieldName = field.getName();

        if (PHONE_FIELD_LIST.contains(fieldName)) {
          field.set(obj, desensPhone((String)field.get(obj)));


        }
        else if (ID_CARD_FIELD_LIST.contains(fieldName)) {
          field.set(obj, desensIdCard((String)field.get(obj)));


        }
        else if (ADDRESS_LIST.contains(fieldName)) {
          field.set(obj, desensAddress((String)field.get(obj)));


        }
        else if (QQ_LIST.contains(fieldName)) {
          field.set(obj, desensQQ((String)field.get(obj)));


        }
        else if (WECHAT_LIST.contains(fieldName)) {
          field.set(obj, desensWechat((String)field.get(obj)));



        }
        else if (TELPHONE_LIST.contains(fieldName)) {
          field.set(obj, desensTelPhone((String)field.get(obj)));
        }
      }
    }
  }
}
